package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.web.http.HttpContentType;
import com.ts.api.http.org.entity.apply.OrgApplyYiRuoJinFu;
import com.ts.api.http.org.entity.dataexchange.OrgDataExchangeYiRuoJinFu;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;


/**
 *  一诺金服
 *
 *  @author WJB
 */
@ApiPlan(productId = "53", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5YiRuoJinFu implements ApiOrgPhoneMd5ConvertHandler {

    private static final String KEY = "9vs7gwx45opdb7e3";
    private static final String IV = "9vs7gwx45opdb7e3";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        apiDataExchangeParameter.setHttpContentType(HttpContentType.APPLICATION_FORM_URLENCODED);
        OrgDataExchangeYiRuoJinFu data = new OrgDataExchangeYiRuoJinFu();
        data.setCity(apiDataExchangeParameter.getCity());
        data.setSource("axfq");
        data.setPhone(apiDataExchangeParameter.getPhoneMd5());
        return data;
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("status"), 200)) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyYiRuoJinFu data = new OrgApplyYiRuoJinFu();
        data.setQdName("axfq");
        data.setPhone(apiApplyParameter.getPhone());
        data.setName(apiApplyParameter.getUserName());
        //  贷款额度
        Integer money = ChooseEq.create()
                .when(apiApplyParameter.getLoanAmount(), "1", 30000)
                .when(apiApplyParameter.getLoanAmount(), "2", 50000)
                .when(apiApplyParameter.getLoanAmount(), "3", 100000)
                .when(apiApplyParameter.getLoanAmount(), "4", 200000)
                .end(30000);
        data.setPrice(money);
        //  房产
        Integer house = ChooseEq.create()
                .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                .when(apiApplyParameter.getRealEstateInfo(), "2", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "3", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "4", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "5", 1)
                .when(apiApplyParameter.getRealEstateInfo(), "6", 1)
                .end(0);
        data.setHouse(house);
        //  车产
        Integer vehicle = ChooseEq.create()
                .when(apiApplyParameter.getCarInfo(), "1", 0)
                .when(apiApplyParameter.getCarInfo(), "2", 1)
                .when(apiApplyParameter.getCarInfo(), "3", 1)
                .when(apiApplyParameter.getCarInfo(), "4", 1)
                .end(0);
        data.setCar(vehicle);
        //  社保
        Integer socialSecurity = ChooseEq.create()
                .when(apiApplyParameter.getSocialSecurity(), "1", 0)
                .when(apiApplyParameter.getSocialSecurity(), "2", 1)
                .when(apiApplyParameter.getSocialSecurity(), "3", 1)
                .when(apiApplyParameter.getSocialSecurity(), "4", 1)
                .end(0);
        data.setSocital(socialSecurity);
        //  公积金
        Integer providentFund = ChooseEq.create()
                .when(apiApplyParameter.getProvidentFund(), "1", 0)
                .when(apiApplyParameter.getProvidentFund(), "2", 1)
                .when(apiApplyParameter.getProvidentFund(), "3", 1)
                .when(apiApplyParameter.getProvidentFund(), "4", 1)
                .end(0);
        data.setFund(providentFund);
        //  商业保险
        Integer insurance = ChooseEq.create()
                .when(apiApplyParameter.getInsurancePolicy(), "1", 0)
                .when(apiApplyParameter.getInsurancePolicy(), "2", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "3", 1)
                .when(apiApplyParameter.getInsurancePolicy(), "4", 1)
                .end(0);
        data.setInsurance(insurance);
        //  信用卡
        Integer creditCard = ChooseEq.create()
                .when(apiApplyParameter.getCreditLimit(), "1", 0)
                .when(apiApplyParameter.getCreditLimit(), "2", 1)
                .when(apiApplyParameter.getCreditLimit(), "3", 1)
                .when(apiApplyParameter.getCreditLimit(), "4", 1)
                .when(apiApplyParameter.getCreditLimit(), "5", 1)
                .when(apiApplyParameter.getCreditLimit(), "6", 1)
                .end(0);
        data.setXyk(creditCard);
        //  微粒贷
        data.setWld(0);
        //  企业纳税
        data.setQyns(0);
        //  工资发放
        Integer salaryPayMethod = ChooseEq.create()
                .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                .when(apiApplyParameter.getSalaryPayMethod(), "2", 0)
                .when(apiApplyParameter.getSalaryPayMethod(), "3", 0)
                .end(0);
        data.setDf(salaryPayMethod);
        //  营业执照
        Integer bizLicenseYear = ChooseEq.create()
                .when(apiApplyParameter.getBizLicenseYear(), "1", 0)
                .when(apiApplyParameter.getBizLicenseYear(), "2", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "3", 1)
                .when(apiApplyParameter.getBizLicenseYear(), "4", 1)
                .end(0);
        data.setBusiness_license(bizLicenseYear);
        //  来源
        data.setSource("axfq");
        //  年龄
        data.setAge(ClassConvertUtils.toInt(apiApplyParameter.getAge()));
        //  城市名称
        data.setCity(apiApplyParameter.getCity());

        AES aes = AES.createCbc(KEY.getBytes(), IV.getBytes());
        return aes.encryptToBase64Str(JsonUtils.toJson(data));
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("status"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
